//
//  MainTainNotificationDetailVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "MainTainNotificationDetailVC.h"
#import "PutNormolForIPhoneCell.h"
#import "SearchForDiseaseForIPhoneCell.h"
#define PutCellID @"cellid1"
#define DetailCellID @"cellid2"
@interface MainTainNotificationDetailVC ()<DetailForIPhoneCellDelegate>
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) UIButton *footerBtn;
@property (nonatomic,strong) NSString *startName;//起始桩号
@property (nonatomic,strong) NSString *endName;//终止桩号

@end

@implementation MainTainNotificationDetailVC

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetNav];
    [self setWebData];
    [self setTableViewFooterView];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"病害巡查编码",@"路线编码",@"行车方向",@"起点桩号",@"终点桩号",@"病害具体情况",@"通知单编码",@"发起部门",@"接收部门",@"维修部门",@"质量要求",@"维修开始日期",@"维修结束日期",@"签发人",@"签发日期"];
    }
    return _titleArray;
}

#pragma mark - UI
- (void)resetNav{
    [self setTheBackBtn];
    self.myTitle.text = @"维修通知单";
    [self.tableView registerClass:[PutNormolForIPhoneCell class] forCellReuseIdentifier:PutCellID];
    [self.tableView registerClass:[SearchForDiseaseForIPhoneCell class] forCellReuseIdentifier:DetailCellID];
}
- (void)setTableViewFooterView{
    
    self.footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200*WidthRate)];
    self.footerView.backgroundColor = [PublicMethod colorWithHexString:@"#eaeaea" andAlpha:1];
    self.tableView.tableFooterView = self.footerView;
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.layer.cornerRadius = 5;
    saveBtn.layer.masksToBounds = YES;
    saveBtn.backgroundColor = MAIN_Color;
    [saveBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    [saveBtn setTitle:@"生成维修单" forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(46*WidthRate);
        make.left.mas_equalTo(25*WidthRate);
        make.right.mas_equalTo(-25*WidthRate);
    }];
    
    if (self.state) {
        self.tableView.tableFooterView.hidden = YES;
    }else{
        self.tableView.tableFooterView.hidden = NO;
    }

}

#pragma mark - UI Response
- (void)btnClick{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确认" message:@"确定生成维修单吗?" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
        [NetWorkTool becomeMaintentanceWithMaintentanceNoticeId:self.dic[@"maintentanceNoticeId"] block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"生成维修单成功" animated:YES];
                    
                    [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                    
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }
                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:actionSheet animated:YES completion:nil];


 
   

}
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Data handle
- (void)setWebData{
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [NetWorkTool getMainTainNotificationDetailWithID:self.codeId block:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"];
                self.startName = [NSString stringWithFormat:@"%@",self.dic[@"startStake"]] ;
                self.endName = [NSString stringWithFormat:@"%@",self.dic[@"endStake"]];
                [self.tableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
#pragma mark - Delegate
- (void)detailForIPhoneCellWithString:(NSString *)str indexPath:(NSIndexPath *)indexPath{
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 15;
            break;
        case 1:
            return 1;
            break;
        default:
            return 1;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 45*WidthRate;
    }else{
        return 135*WidthRate;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        PutNormolForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
        cell.titleLab.text = self.titleArray[indexPath.row];
        switch (indexPath.row) {
            case 0:
            {
                cell.contentLab.text = self.dic[@"diseaseInspectionNo"];
            }
                break;
            case 1:
            {
                 cell.contentLab.text = self.dic[@"routeCode"];
            }
                break;
            case 2:
            {
                 cell.contentLab.text = self.dic[@"drivingDirectionName"];
            }
                break;
            case 3:
            {
                NSString *num = [NSString stringWithFormat:@"%.3f",[self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000];
                if ([num integerValue] == [num floatValue]) {
                    num = [NSString stringWithFormat:@"%ld",[num integerValue]];
                }
                if (([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)<10) {
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+00%@",[self.startName integerValue]/1000,num];
                }else if(([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)>=10&&([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)<100){
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+0%@",[self.startName integerValue]/1000,num];
                }else{
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+%@",[self.startName integerValue]/1000,num];
                }
            }
                break;
            case 4:
            {
                NSString *num = [NSString stringWithFormat:@"%.3f",[self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000];
                if ([num integerValue] == [num floatValue]) {
                    num = [NSString stringWithFormat:@"%ld",[num integerValue]];
                }
                if (([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)<10) {
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+00%@",[self.endName integerValue]/1000,num];
                }else if(([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)>=10&&([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)<100){
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+0%@",[self.endName integerValue]/1000,num];
                }else{
                    cell.contentLab.text = [NSString stringWithFormat:@"K%ld+%@",[self.endName integerValue]/1000,num];
                }

            }
                break;
            case 5:
            {
                
                    if ([self.dic[@"diseaseLevelName"] length]==0) {
                        cell.contentLab.text = [NSString stringWithFormat:@"%@%@%@%@",self.dic[@"diseaseLevelName"],self.dic[@"diseaseFormName"],[PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.dic[@"num"] doubleValue]]],self.dic[@"measureUnitName"]];
                    }else{
                        cell.contentLab.text = [NSString stringWithFormat:@"%@度%@%@%@",self.dic[@"diseaseLevelName"],self.dic[@"diseaseFormName"],[PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.dic[@"num"] doubleValue]]],self.dic[@"measureUnitName"]];
                    }
                
            }
                break;
            case 6:
            {
                 cell.contentLab.text = self.dic[@"billNo"];
            }
                break;
            case 7:
            {
                 cell.contentLab.text = self.dic[@"lanuchDepartmentName"];
            }
                break;
            case 8:
            {
                 cell.contentLab.text = self.dic[@"receiveDepartmentName"];
            }
                break;
            case 9:
            {
                 cell.contentLab.text = self.dic[@"maintenanceDepartmentName"];
            }
                break;
            case 10:
            {
                 cell.contentLab.text = self.dic[@"qualityRequirement"];
            }
                break;
            case 11:
            {
                 cell.contentLab.text = self.dic[@"startDate"];
            }
                break;
            case 12:
            {
                cell.contentLab.text = self.dic[@"endDate"];
            }
                break;
            case 13:
            {
                cell.contentLab.text = self.dic[@"userName"];
            }
                break;
            case 14:
            {
                cell.contentLab.text = self.dic[@"signDate"];
            }
                break;
            default:
                break;
        }
        cell.img.hidden = YES;
        cell.img.image = [UIImage imageNamed:@"enter"];
        
        return  cell;
    }else{
        SearchForDiseaseForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
        cell.delegate = self;
        cell.indexPath = indexPath;
        if (indexPath.section ==1) {
           cell.titleLab.text = @"日常养护措施";
           cell.detailTextView.text = self.dic[@"measureName"];
        }else{
           cell.titleLab.text = @"备注";
           cell.detailTextView.text = self.dic[@"remark"];
        }
        
        cell.detailTextView.placeholder = @" ";
        cell.detailTextView.editable = NO;
        
        return  cell;
    }
}

@end
